package cn.dnui_wnx725.dnuifood_16110100725.controller;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import cn.dnui_wnx725.dnuifood_16110100725.R;
import cn.dnui_wnx725.dnuifood_16110100725.bean.UserBean;
import cn.dnui_wnx725.dnuifood_16110100725.listener.TListener;
import cn.dnui_wnx725.dnuifood_16110100725.model.LoginModel;


public class LoginActivity extends BaseActivity {
    EditText et_uname,et_pw;
    Button btn_login,btn_regist;
    CheckBox checkBox_remember;
    String userid;
    private SharedPreferences sp;
    TListener<UserBean> tListener = new TListener<UserBean>() {
        @Override
        public void onResponse(UserBean userBean) {
            userid=userBean.getUserid();
            if(userBean.getUserid().equals("0")){
                Toast.makeText(getApplicationContext(),"Login Filed",Toast.LENGTH_SHORT).show();
            }else {
//        Toast.makeText(LoginActivity.this, userBean.getUserid().toString(), Toast.LENGTH_SHORT).show();
                showToast(userid);
                saveUser();
                Intent intent =new Intent(LoginActivity.this,MainActivity.class);
                startActivityForResult(intent,2);
                finish();}
        }

        @Override
        public void onFail(String msg) {
        }
    };

    @Override
    void initView() {
        setLayout_file(R.layout.activity_login);
        et_uname=(EditText)findViewById(R.id.et_uname);
        et_pw=(EditText)findViewById(R.id.et_pw);
        btn_login=(Button)findViewById(R.id.btn_login);
        btn_regist=(Button)findViewById(R.id.btn_regist);
        checkBox_remember=(CheckBox)findViewById(R.id.checkbox_remember);
        boolean remember = getUser_remember();
        if(remember==true) {
            String username = getUser_name();
            String userpass = getUser_pw();
            et_uname.setText(username);
            et_pw.setText(userpass);
            checkBox_remember.setChecked(remember);
        }
    }

    @Override
    void initEvent() {
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(et_uname.getText().toString().trim().equals("")||et_pw.getText().toString().trim().equals("")){
                    //用户名和密码为空
                    Toast.makeText(LoginActivity.this,"CAN NOT BE NULL!!",Toast.LENGTH_SHORT).show();
                }else {
                    //拿着用户名和密码区服务器验证
//                    Toast.makeText(LoginActivity.this,"Login successfully!",Toast.LENGTH_SHORT).show();
                    String username = et_uname.getText().toString();
                    String userpass = et_pw.getText().toString();
                    LoginModel model = new LoginModel();
                    model.getLoginMessge(username,userpass, tListener);
                }
            }
        });

        btn_regist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent,1);
//                finish();
            }
        });

    }

    @Override
    void initData() {

    }


    public  void saveUser(){
        sp = getSharedPreferences("userInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("username", et_uname.getText().toString());
        editor.putBoolean("isChecked",checkBox_remember.isChecked());
        editor.putString("userpass", et_pw.getText().toString());
        editor.putString("userid", userid);
        showToast(userid);
        editor.commit();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 1:
                if(resultCode== RESULT_OK){
                    String string=data.getStringExtra("uname");
                    showToast(string);
                    et_uname.setText(string);
                }
        }
    }

}
